

var mc = null;
var mc1 = null;
var ltln = null;
var map = null;
var crc = [];
var mgr = null;

var markers = [];

var incid = [];
var pts = [];


var specialmarker=null;


/**
 * Toggles Marker Manager visibility.
 */
function toggleMarkerManager() {
  showMarketManager = !showMarketManager;
  if (mgr) {
    if (showMarketManager) {
      mgr.addMarkers(markers.countries, 0, 5);
      mgr.addMarkers(markers.places, 6, 11);
      mgr.addMarkers(markers.locations, 12);
      mgr.refresh();
    } else {
      mgr.clearMarkers();
      mgr.refresh();
    }
  } else {
    mgr = new MarkerManager(map, {trackMarkers: true, maxZoom: 15});
    google.maps.event.addListener(mgr, 'loaded', function() {
      mgr.addMarkers(markers.countries, 0, 5);
      mgr.addMarkers(markers.places, 6, 11);
      mgr.addMarkers(markers.locations, 12);
      mgr.refresh();
    });
  }
}


/**
 * Toggles Marker Clusterer visibility.
 */
function toggleMarkerClusterer() {
  showMarketClusterer = !showMarketClusterer;
  if (showMarketClusterer) {
    if (mc) {
      mc.addMarkers(markers.locations);
    } else {
      mc = new MarkerClusterer(map, markers.locations, {maxZoom: 19});
    }
  } else {
    mc.clearMarkers();
  }
}


/**
 * Initializes the map and listeners.
 */
function initialize() 
{
    var latlng = new google.maps.LatLng(27, -27)
    var myOptions = {
        zoom: 2,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    if (Points) {
        for (var level in Points) {
            for (var i = 0; i < Points[level].length; i++) {
                var details = Points[level][i];
                incid[i] = new google.maps.Marker({
                    title: details.level,
                    position: new google.maps.LatLng(details.location[0], details.location[1]),
                    clickable: true,
                    draggable: false,
                    flat: true
                });
/*		    crc[i] = new google.maps.Circle({
  map: map,
  radius: 1000,    // metres
  fillColor: '#AA0000'
});
crc[i].bindTo('center', incid[i], 'position'); */
            }
        }
    }


    if (data) {
            for (var i = 0; i < data.count; i++) {
                var details = data.genomes[i];
                pts[i] = new google.maps.Marker({
                    title: details.organism_name,
                    position: new google.maps.LatLng(details.latitude, details.longitude),
                    clickable: true,
                    draggable: false,
                    flat: true
                });
            }
    }
    

    drawChart();
}
      
function dist() {
       var distanceWidget = new DistanceWidget(map);
}

function DistanceWidget(map) {
        this.set('map', map);
        this.set('position', map.getCenter());

        var marker = new google.maps.Marker({
          draggable: true,
          title: 'Move me!'
        });

        // Bind the marker map property to the DistanceWidget map property
        marker.bindTo('map', this);

        // Bind the marker position property to the DistanceWidget position
        // property
        marker.bindTo('position', this);

        // Create a new radius widget
        var radiusWidget = new RadiusWidget();

        // Bind the radiusWidget map to the DistanceWidget map
        radiusWidget.bindTo('map', this);

        // Bind the radiusWidget center to the DistanceWidget position
        radiusWidget.bindTo('center', this, 'position');

        // Bind to the radiusWidgets' distance property
        this.bindTo('distance', radiusWidget);

        // Bind to the radiusWidgets' bounds property
        this.bindTo('bounds', radiusWidget);
      }
      DistanceWidget.prototype = new google.maps.MVCObject();


      /**
       * A radius widget that add a circle to a map and centers on a marker.
       *
       * @constructor
       */
      function RadiusWidget() {
        var circle = new google.maps.Circle({
          strokeWeight: 2
        });

        // Set the distance property value, default to 50km.
        this.set('distance', 50);

        // Bind the RadiusWidget bounds property to the circle bounds property.
        this.bindTo('bounds', circle);

        // Bind the circle center to the RadiusWidget center property
        circle.bindTo('center', this);

        // Bind the circle map to the RadiusWidget map
        circle.bindTo('map', this);

        // Bind the circle radius property to the RadiusWidget radius property
        circle.bindTo('radius', this);

        // Add the sizer marker
        this.addSizer_();
      }
      RadiusWidget.prototype = new google.maps.MVCObject();


      /**
       * Update the radius when the distance has changed.
       */
      RadiusWidget.prototype.distance_changed = function() {
        this.set('radius', this.get('distance') * 1000);
      };


      /**
       * Add the sizer marker to the map.
       *
       * @private
       */
      RadiusWidget.prototype.addSizer_ = function() {
        var sizer = new google.maps.Marker({
          draggable: true,
          title: 'Set radius',
	  icon: 'bullet_red.png'
        });

        sizer.bindTo('map', this);
        sizer.bindTo('position', this, 'sizer_position');

        var me = this;
        google.maps.event.addListener(sizer, 'drag', function() {
          // Set the circle distance (radius)
          me.setDistance();
        });
      };


      /**
       * Update the center of the circle and position the sizer back on the line.
       *
       * Position is bound to the DistanceWidget so this is expected to change when
       * the position of the distance widget is changed.
       */
      RadiusWidget.prototype.center_changed = function() {
        var bounds = this.get('bounds');

        // Bounds might not always be set so check that it exists first.
        if (bounds) {
          var lng = bounds.getNorthEast().lng();

          // Put the sizer at center, right on the circle.
          var position = new google.maps.LatLng(this.get('center').lat(), lng);
          this.set('sizer_position', position);
        }
      };


      /**
       * Calculates the distance between two latlng points in km.
       * @see http://www.movable-type.co.uk/scripts/latlong.html
       *
       * @param {google.maps.LatLng} p1 The first lat lng point.
       * @param {google.maps.LatLng} p2 The second lat lng point.
       * @return {number} The distance between the two points in km.
       * @private
       */
      RadiusWidget.prototype.distanceBetweenPoints_ = function(p1, p2) {
        if (!p1 || !p2) {
          return 0;
        }

        var R = 6371; // Radius of the Earth in km
        var dLat = (p2.lat() - p1.lat()) * Math.PI / 180;
        var dLon = (p2.lng() - p1.lng()) * Math.PI / 180;
        var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
          Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) *
          Math.sin(dLon / 2) * Math.sin(dLon / 2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        var d = R * c;
        return d;
      };


      /**
       * Set the distance of the circle based on the position of the sizer.
       */
      RadiusWidget.prototype.setDistance = function() {
        // As the sizer is being dragged, its position changes.  Because the
        // RadiusWidget's sizer_position is bound to the sizer's position, it will
        // change as well.
        var pos = this.get('sizer_position');
        var center = this.get('center');
        var distance = this.distanceBetweenPoints_(center, pos);

        // Set the distance property for any objects that are bound to it
        this.set('distance', distance);
      };

/**
*  for the postion, you can't put (0.0, 0.1)
*  it MUST be of the type google.maps.LatLng...
*/
function markPts() {
for (var i = 0; i < incid.length; i++) {
		    crc[i] = new google.maps.Circle({
  map: map,
  radius: 2500,    // metres
  fillColor: '#AA0000'
});
crc[i].bindTo('center', incid[i], 'position');}

    var mcOptions = { gridSize: 50, maxZoom: 15 };
    mgr = new MarkerClusterer(map, incid);

}

function markDies() {
    if (mc1) {
        mc1.addMarkers(markers.locations);
    } else {
        mc1 = new MarkerClusterer(map, pts.locations, { maxZoom: 19 });
    }
    mgr = new MarkerClusterer(map, pts);
}

function special() {
	if (!specialmarker)
     {
 	specialmarker = new google.maps.Marker({
	position: new google.maps.LatLng(30,-20),
	draggable: true,
	title: 'Search Center',
	icon: "restaurant.png"
	});
   var but = document.getElementById('SearchCenter')
   but.Enabled="disabled";
   specialmarker.setMap(map)
  }
}
 

function markEats() { 

    var searchControl = new google.search.SearchControl(null);
    var localSearch = new google.search.LocalSearch();
   
    // set the local search center point to match the map
    if (specialmarker)
   ltln = specialmarker.getPosition()
    if (ltln) { 
    localSearch.setCenterPoint(ltln);

    var controlContainer = document.getElementById('searchcontrol');  

    var options = new google.search.SearcherOptions();
    options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

    searchControl.addSearcher(localSearch, options);
/**
*    searchControl.setSearchCompleteCallback(this, function () {
*
*        var results = searcher.results; // Grab the results array
*        alert(results);
*        // We loop through to get the points
*        for (var i = 0; i < results.length; i++) {
*            var result = results[i]; // Get the specific result
*            var markerLatLng = new google.maps.LatLng(parseFloat(result.lat),
*                                                parseFloat(result.lng));
*            var marker = new google.maps.Marker(markerLatLng); // Create the marker
* 
*            // Bind information for the infoWindow aka the map marker popup
*            marker.bindInfoWindow(result.html.cloneNode(true));
*            result.marker = marker; // bind the marker to the result
*            map.addOverlay(marker); // add the marker to the map
*        }
*    });
*/  
    searchControl.draw(controlContainer);
    searchControl.execute("restaurants");
}
}
function gotResults(sc, searcher) {

   
    var resultcontent = '';
    var resultdiv = document.getElementById('searchcontrol');  
    for (i=0; i<searcher.results.length; i++)  
    {  
       var result = searcher.results[i];  
           resultcontent += '<p>'+result.title+'<br />'+result.streetAddress+'</p>';
      }
       resultdiv.nodeValue = 'TEst';// resultcontent;  
}

function drawChart() {
    var dataPts = new google.visualization.DataTable();
    dataPts.addColumn('string', 'Days of Infection');
    dataPts.addColumn('number', 'Number of Cases');
    dataPts.addRows(9);
    dataPts.setValue(0, 0, '0');
    dataPts.setValue(0, 1, 100);
    dataPts.setValue(1, 0, '1');
    dataPts.setValue(1, 1, 200);
    dataPts.setValue(2, 0, '2');
    dataPts.setValue(2, 1, 400);
    dataPts.setValue(3, 0, '3');
    dataPts.setValue(3, 1, 720);
    dataPts.setValue(4, 0, '4');
    dataPts.setValue(4, 1, 800);
    dataPts.setValue(5, 0, '5');
    dataPts.setValue(5, 1, 700);
    dataPts.setValue(6, 0, '6');
    dataPts.setValue(6, 1, 400);
    dataPts.setValue(7, 0, '6');
    dataPts.setValue(7, 1, 150);
    dataPts.setValue(8, 0, '6');
    dataPts.setValue(8, 1, 99);
    var chart = new google.visualization.LineChart(document.getElementById('chart_canvas'));
    chart.draw(dataPts, { width: 800, height: 300, title: 'Cases versus Time(Days)' });
} 

google.maps.event.addDomListener(window, 'load', initialize);

